package com.qf.flashlib.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

import com.qf.flashlib.R;


/**
 * 创建日期：2019/8/18 on 15:52
 * 描述:
 * 作者:戴旭东
 */
public class CircleImageView extends AppCompatImageView {
    private static final String TAG = "circleImageView";
    private int width, height;

    private Paint mPaint;

    private PorterDuffXfermode xfermode;

    private Path path;

    private float topLeftRadius;
    private float topRightRadius;
    private float bottomLeftRadius;
    private float bottomRightRadius;
    private boolean canRotate;

    RectF rectTopLeft;
    RectF rectTopRight;
    RectF rectbottomLeft;
    RectF rectBottright;

    Context mContext;

    private ValueAnimator valueAnimator;
    private float degree;

    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView);
        topLeftRadius = typedArray.getDimension(R.styleable.CircleImageView_civTopLeft, 0);
        topRightRadius = typedArray.getDimension(R.styleable.CircleImageView_civTopRight, 0);
        bottomLeftRadius = typedArray.getDimension(R.styleable.CircleImageView_civBottomLeft, 0);
        bottomRightRadius = typedArray.getDimension(R.styleable.CircleImageView_civBottomRight, 0);
        canRotate = typedArray.getBoolean(R.styleable.CircleImageView_civCanRotate, false);
        typedArray.recycle();
        this.mContext = context;

        init(context);
    }

    private void init(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.RED);
        xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);


        path = new Path();

        valueAnimator = ValueAnimator.ofFloat(0, 360);
        valueAnimator.setDuration(10000);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);


    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.width = w;
        this.height = h;

        rectTopLeft = new RectF(0, 0, topLeftRadius * 2, topLeftRadius * 2);
        rectTopRight = new RectF(w - topRightRadius * 2, 0, w, topLeftRadius * 2);
        rectbottomLeft = new RectF(0, height - bottomLeftRadius * 2, bottomLeftRadius * 2, height);
        rectBottright = new RectF(w - bottomRightRadius * 2, height - bottomRightRadius * 2, w, height);


        path.moveTo(0, getHeight() - bottomLeftRadius);
        path.lineTo(0, topLeftRadius);
        path.arcTo(rectTopLeft, 180, 90);
        path.lineTo(width - topRightRadius, 0);
        path.arcTo(rectTopRight, 270, 90);
        path.lineTo(width, height - bottomRightRadius);
        path.arcTo(rectBottright, 0, 90);
        path.lineTo(bottomLeftRadius, height);
        path.arcTo(rectbottomLeft, 90, 90);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        setLayerType(LAYER_TYPE_HARDWARE, null);
        int i = canvas.saveLayer(0, 0, width, height, null, Canvas.ALL_SAVE_FLAG);
//        canvas.rotate(90, getWidth()/ 2, getHeight() /2);
        super.onDraw(canvas);
        mPaint.setXfermode(xfermode);
        canvas.drawPath(path, mPaint);
        mPaint.setXfermode(null);

        canvas.restoreToCount(i);

    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                degree = (float) animation.getAnimatedValue();
                invalidate();
                CircleImageView.this.setRotation(degree);
            }
        });
        if (canRotate) {
            valueAnimator.start();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (valueAnimator != null && valueAnimator.isRunning())
            valueAnimator.cancel();
    }
}
